import http from './axios'

// 将base64转换为blob
function convertBase64UrlToBlob (urlData) {
  let bytes = window.atob(urlData) // 去掉url的头，并转换为byte
  // 处理异常,将ascii码小于0的转换为大于0
  let ab = new ArrayBuffer(bytes.length)
  let ia = new Uint8Array(ab)
  for (let i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i)
  }
  return new Blob([ab], { type: 'image/jpeg' })
}

export const uploadFace = (params = {}) => {
  let image = params.image
  let base64 = image.slice(image.indexOf(',') + 1)
  let formData = new FormData()
  formData.append('merge_file', convertBase64UrlToBlob(base64), 'file.jpeg')
  return http({
    method: 'post',
    url: '/api/upload_image',
    data: formData
  })
}

export const mergeFace = (params = {}) => {
  return http({
    method: 'post',
    url: '/api/merge_face',
    data: params
  })
}

export default {
  mergeFace,
  uploadFace
}
